package com.example.menuvirtualclient.repository;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import com.example.menuvirtualclient.core.*;
import com.example.menuvirtualclient.data.Repository;
import com.example.menuvirtualclient.util.Common;

public class CommentRepository extends Repository<Comment>{

private static final String LOGTAG = "CommentRepository";
	
	public CommentRepository(Context context) {
		super(context);
	}
	
	@Override
	public Comment Get(long id) {
		SQLiteDatabase db = null;
		Comment entity = null;
		String[] filter = new String[1];
		filter[0] = Long.toString(id);
		try{
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();			
			Cursor cursor = db.rawQuery("SELECT id, description, price FROM Comments WHERE id = ?", filter);
			
			cursor.moveToFirst();
			entity = BuildNewEntity(cursor); 
			cursor.close();
		    db.close();
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());			
		}
		finally{
			if(db != null)
				if(db.isOpen()) {
					db.close();
					Log.e(LOGTAG, "Close database");		
				}
		}
		return entity;
	}
	
	@Override
	public void Insert(Comment entity) {
		SQLiteDatabase db = null;
		try{
			ContentValues newRow = new ContentValues();
			newRow.put("id", entity.getId());			
			newRow.put("description", entity.getDescription());			
			newRow.put("price", entity.getPrice());
			
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();
			db.insert(getTableName(), null, newRow);
			
			db.close();
		}		
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());		
		}
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
			_session.Close();
		}
	}
	
	@Override
	public boolean Update(Comment entity) {
		boolean result = false;
		SQLiteDatabase db = null;
		try{
			ContentValues values = new ContentValues();
			values.put("description", entity.getDescription());
			values.put("price", entity.getPrice());
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();
			int rows = db.update(getTableName(), values, "id = " + entity.getId(), null);			
			result = (rows > 0) ? true : false;
			db.close();
		}		
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
		return result;
	}
	
	public boolean Delete(){
		boolean result = false;
		SQLiteDatabase db = null;

		try{
			if(!_session.IsOpen())
				_session.Open();

			db = _session.Connection();
			int rows = db.delete(getTableName(), null, null);
			result = (rows > 0) ? true : false;
			db.close();			
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
		return result;
	}
	
	public boolean DeleteDishComments(){
		boolean result = false;
		SQLiteDatabase db = null;

		try{
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();			
			int rows = db.delete("Dishes_Comments", null, null);
			result = (rows > 0) ? true : false;
			db.close();			
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
		return result;
	}
	
	public boolean DeleteOrderDetailComments(){
		boolean result = false;
		SQLiteDatabase db = null;

		try{
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();
			int rows = db.delete("OrderDetails_Comments", null, null);
			result = (rows > 0) ? true : false;
			db.close();			
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
		return result;
	}
	
	public boolean deleteOrderDetailCommentsById(int detailId){
		boolean result = false;
		SQLiteDatabase db = null;

		try{
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();
			int rows = db.delete("OrderDetails_Comments", "orderdetailid=?", new String[] { String.valueOf(detailId)});
			result = (rows > 0) ? true : false;
			db.close();			
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
		return result;
	}
	
	
	public List<Comment> getAllbyOrderDetail(int orderDetailId){
		SQLiteDatabase db = null;
		try{
			List<Comment> list = new ArrayList<Comment>();
			String[] filter = new String[1];
			filter[0] = Long.toString(orderDetailId);
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();
			Cursor cursor = db.rawQuery("SELECT c.id, c.description, c.price FROM OrderDetails_Comments oc INNER JOIN Comments c ON c.id = oc.commentid WHERE oc.orderdetailid = ? ", filter);
			
			cursor.moveToFirst();
		    while (!cursor.isAfterLast()) {
		    	list.add(BuildNewEntity(cursor));
		    	cursor.moveToNext();
		    }
		    cursor.close();
		    db.close();
		    return list;
		}
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
			throw ex;
		}		
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
		}
	}
	
	public void saveOrderDetailComment(int orderdetailid, int commentid) {
		SQLiteDatabase db = null;
		try{
			ContentValues newRow = new ContentValues();
			newRow.put("orderdetailid", orderdetailid);			
			newRow.put("commentid", commentid);			
			
			if(!_session.IsOpen())
				_session.Open();
			
			db = _session.Connection();
			db.insert("OrderDetails_Comments", null, newRow);
			
			db.close();
		}		
		catch(SQLiteException ex){
			Log.e(LOGTAG, ex.getMessage());
		}
		catch(Exception ex){
			Log.e(LOGTAG, ex.getMessage());		
		}
		finally{
			if(db != null)
				if(db.isOpen()) db.close();
			_session.Close();
		}
	}
	
	@Override
	public String getTableName() {
		return "Comments";
	}
	
	@Override
	public Comment BuildNewEntity(Cursor cursor) {
		Comment comment = new Comment();
		comment.setId(cursor.getInt(0));		
		comment.setDescription(cursor.getString(1));
		double price = Common.Round(cursor.getDouble(2), 2); 
		comment.setPrice(price);

		return comment;
	}
	
}
